import {Injectable} from '@nestjs/common';
import {InjectRepository} from "@nestjs/typeorm";
import {SysConfigEntity} from "./entities/sysConfig.entity";
import {Repository} from "typeorm";
import {MsgService} from "@/modules/common/msg/msg.service";
import {SysConfigUpdateDto} from "@/modules/sysConfig/dto";

@Injectable()
export class SysConfigService {
    constructor(@InjectRepository(SysConfigEntity) private readonly sysConfigEntity:Repository<SysConfigEntity>,
                private readonly msgService:MsgService) {
    }

    /**
     * 系统配置
     * @param key
     */
    async sysConfig(key) {
          let sysConfigData;
          try {
              sysConfigData = await this.sysConfigEntity.createQueryBuilder("sysConfig").select(["value"]).where("sysConfig.key = :key",{key}).getRawOne()
          }catch (error) {
              return this.msgService.fail(error);
          }
          return this.msgService.success(sysConfigData);
    }


    /**
     * 更新
     * @param sysConfigUpdateDto
     */
    async sysConfigUpdate(sysConfigUpdateDto:SysConfigUpdateDto) {
        let {key,value} = sysConfigUpdateDto
        try {
            await this.sysConfigEntity.createQueryBuilder().where("key = :key", { key }).update().set({value}).execute();
        }catch (error) {
            return this.msgService.fail(error);
        }
        return this.msgService.success();
    }
}
